package net.xdclass.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.CouponCategoryEnum;
import net.xdclass.request.NewUserRequest;
import net.xdclass.service.ICouponService;
import net.xdclass.utils.JsonData;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lixin
 * @since 2024-08-23
 */
@Api(tags = "优惠卷模块")
@RestController
@RequestMapping("/api/coupon/v1")
@Slf4j
public class CouponController {

    @Autowired
    private ICouponService couponService;

    @ApiOperation("分页查询优惠卷")
    @GetMapping("/page_coupon")
    public JsonData pageCouponList(
            @ApiParam("当前页")
            @RequestParam(value = "page", defaultValue = "1") int page,

            @ApiParam("每页显示多少条")
            @RequestParam(value = "size", defaultValue = "10") int size
    ){
        Map<String, Object> pageMap = couponService.pageCouponActivity(page, size);
        return JsonData.buildSuccess(pageMap);
    }

    /**
     * 领取优惠卷
     * @param couponId
     * @return
     */
    @ApiOperation("领取优惠卷")
    @GetMapping("/add/promotion/{coupon_id}")
    public JsonData addPromotionCoupon(@ApiParam(value = "优惠卷id", required = true) @PathVariable("coupon_id") long couponId){

        JsonData jsonData = couponService.addCoupon(couponId, CouponCategoryEnum.PROMOTION);
        return jsonData;
    }


    @ApiOperation("RPC0-新用户注册接口")
    @PostMapping("/new_user_coupon")
    public JsonData addNewUserCoupon(@ApiParam("用户对象") @RequestBody NewUserRequest newUserRequest){

        JsonData jsonData = couponService.initNewUserCoupon(newUserRequest);
        return JsonData.buildSuccess(jsonData);
    }
}
